//
//  CLImageScrollDisplayCell.m
//  CLImagePicker
//
//  Created by Criss on 2017/4/7.
//  Copyright © 2017年 Criss. All rights reserved.
//

#import "CLImageScrollDisplayCell.h"
#import "CLImageCollectionCell.h"

@implementation CLImageScrollDisplayCell {
    UIImageView *_imageView;
    UIButton *_closeBtn;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    _imageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageView];
    
    _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_closeBtn setImage:obtainImage(@"imageClose") forState:UIControlStateNormal];
    [_closeBtn addTarget:self action:@selector(closeDisplayView:) forControlEvents:UIControlEventTouchUpInside];
    [_closeBtn sizeToFit];
    [self.contentView addSubview:_closeBtn];
    CGFloat width = _closeBtn.bounds.size.width;
    CGFloat height = _closeBtn.bounds.size.height;
    
    _closeBtn.frame = CGRectMake(self.bounds.size.width - width - 10, 30, width * 0.8, height * 0.8);
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self.contentView addGestureRecognizer:tap];
    
}

- (void)tapGesture:(UITapGestureRecognizer *)tap {
    NSLog(@"%s", __func__);
}

- (void)closeDisplayView:(UIButton *)sender {
    if (self.closeBtnClick) {
        self.closeBtnClick();
    }
}

- (void)setDisplayImage:(UIImage *)displayImage {
    _displayImage = displayImage;
    _imageView.image = displayImage;
    if (!CGRectIsEmpty(self.convertFrame) && !CGRectIsNull(self.convertFrame)) {
        _imageView.frame = self.convertFrame;
        [UIView animateWithDuration:0.4 animations:^{
            [self showImageView];
        }];
    }else {
        [self showImageView];
    }
    
}

- (void)showImageView {
    CGRect rect = _imageView.frame;
    rect.size.width = [UIScreen mainScreen].bounds.size.width;
    rect.size.height = rect.size.width / _imageView.image.size.width * _imageView.image.size.height;
    _imageView.frame = rect;
    _imageView.center = self.contentView.center;
}

- (UIImageView *)displayImageView {
    return _imageView;
}

@end
